package com.dotflat.delegation.controller;

import com.dotflat.delegation.domain.FeedDelegation;
import com.dotflat.delegation.po.FeedDelegationPO;
import com.dotflat.delegation.service.FeedDelegationService;
import com.dotflat.rest.controller.WriteableRestfulBaseController;
import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.vo.BindedBabyVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/v2/delegation/feed")
@Slf4j
@Api(tags = "委托 - 喂养")
public class APIFeedDelegationController
        extends WriteableRestfulBaseController<FeedDelegation, FeedDelegationService, FeedDelegationPO> {

    @Autowired
    public void setBaseService(FeedDelegationService service){
        super.setBaseService(service);
    }

    @ApiOperation(value = "增加一条记录")
    @PostMapping()
    @Override
    public StatusResponse add(HttpServletRequest req,
                              @RequestHeader(TICKET) String ticket, FeedDelegationPO param){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");
        BindedBabyVO defaultBaby = loginUser.getDefaultBaby();

        FeedDelegation ret = baseService.add(defaultBaby.getAreaId(), defaultBaby.getId(), param, delegation->{
            delegation.setChildName(defaultBaby.getName());
        });

        List<FeedDelegation> tmp = new ArrayList<>();

        tmp.add(ret);

        return StatusResponse.success(tmp);
    }
}
